package controller;

import javafx.event.ActionEvent;
import javafx.geometry.Side;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import view.AddGroupView;
import view.OnLineView;
import view.SearchFriendOrGroupView;

import java.io.IOException;

public class OnLineController {
    public AnchorPane onLinePane;
    public Button friendBtn;
    public Button groupBtn;
    public Button addBtn;
    public ListView groupList;
    public ListView friendList;
    public Label fTitleName;
    public AnchorPane friendPane;
    public AnchorPane groupPane;
    public Label gTitleName;

    /**
     * 添加好友或添加群聊
     * @param actionEvent
     */
    public void addFriendOrGroup(ActionEvent actionEvent) {
        ContextMenu menu = new ContextMenu();
        MenuItem addFriend = new MenuItem("加好友/加群");
        MenuItem addGroup = new MenuItem("创建群聊");
        menu.getItems().add(addFriend);
        menu.getItems().add(addGroup);
        menu.show(addBtn, Side.LEFT, 0, 0);
        addFriend.setOnAction(event -> {
            System.out.println(addFriend.getText());
            try {
                OnLineView onLineView = (OnLineView) onLinePane.getScene().getWindow();
                new SearchFriendOrGroupView(onLineView).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        addGroup.setOnAction(event -> {
            System.out.println(addGroup.getText());
            try {
                OnLineView onLineView = (OnLineView) onLinePane.getScene().getWindow();
                new AddGroupView(onLineView).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public void clickGroups(ActionEvent actionEvent) {
        groupList.setVisible(true);
        friendList.setVisible(false);
    }

    public void clickFriends(ActionEvent actionEvent) throws IOException {
        groupList.setVisible(false);
        friendList.setVisible(true);
    }

    /**
     * 跳转到好友界面并加载数据
     * @param mouseEvent
     */
    public void jumpPageF(MouseEvent mouseEvent) {
        if (groupPane.isVisible()) {
            groupPane.setVisible(false);
        }
        if (!friendPane.isVisible()) {
            friendPane.setVisible(true);
        }
        Pane pane = (Pane) friendList.getSelectionModel().selectedItemProperty().get();
        Label label = (Label) pane.lookup("#name");
        Label label1 = (Label) pane.lookup("#msg");
        System.out.println(label.getText() + "---" + label1.getText());

        OnLineView onLineView = (OnLineView) onLinePane.getScene().getWindow();
        fTitleName.setText(label.getText());
        onLineView.onloadMsg(label.getText());
    }

    /**
     * 跳转到群聊界面，并加载数据
     * @param mouseEvent
     */
    public void jumpPageG(MouseEvent mouseEvent) {
        if (friendPane.isVisible()) {
            friendPane.setVisible(false);
        }
        if (!groupPane.isVisible()) {
            groupPane.setVisible(true);
        }
        Pane pane = (Pane) groupList.getSelectionModel().selectedItemProperty().get();
        Label label = (Label) pane.lookup("#name");
        Label label1 = (Label) pane.lookup("#msg");
        System.out.println(label.getText() + "---" + label1.getText());

        OnLineView onLineView = (OnLineView) onLinePane.getScene().getWindow();
        gTitleName.setText(label.getText());
        System.out.println("controller----" + label.getText());
        onLineView.onloadGroupMsg(label.getText());
        onLineView.onloadGroupMember(label.getText());
    }
}
